home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 4 / FM Towns Free Software Collection 4 - Disc 1.iso / t_os / whisper / source / file.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-19  |  7.0 KB  |  379 lines

  1. #include    <stdio.h>
  2. #include    <stdlib.h>
  3. #include    <string.h>
  4. #include    <ctype.h>
  5. #include    "graphic.h"
  6. #include    "coldef.h"
  7.  
  8. #define    TRUE    1
  9. #define    FALSE    0
  10. #define    ERR    (-1)
  11.  
  12. #define    TAB    8
  13.  
  14. #define    TYPE_Y    20
  15. #define    TYPE_N    8
  16.  
  17. #define    unlink(f) remove(f)
  18.  
  19. #define    SECRET    "SECRET\x1A"
  20.  
  21. extern short t_crc;
  22.  
  23. char    *macget(char *mac);
  24. int    getdir(char *name);
  25.  
  26. char    *read_line(char tmp[],FILE *fp)
  27. {
  28.     int     i,j,ch;
  29.  
  30.     for ( i = 0 ; i < 80 && (ch = getc(fp)) != EOF ; ) {
  31.  
  32. RECHK:
  33.         if ( ch == '\n' ) {
  34.         tmp[i++] = ch;
  35.         break;
  36.  
  37.         } else if ( ch == '\t' ) {
  38.         j = TAB - (i % TAB);
  39.         while ( i < 80 && j-- > 0 )
  40.         tmp[i++] = ' ';
  41.  
  42.     } else if ( iskanji(ch) ) {
  43.             if ( i == (80 - 1) ) {
  44.                 ungetc(ch,fp);
  45.         break;
  46.             }
  47.         tmp[i++] = ch;
  48.  
  49.             if ( (ch = getc(fp)) == EOF )
  50.         break;
  51.         else if ( iskanji2(ch) )
  52.                 tmp[i++] = ch;
  53.         else
  54.         goto RECHK;
  55.  
  56.     } else if ( ch != '\0' )
  57.         tmp[i++] = ch;
  58.     }
  59.     tmp[i] = '\0';
  60.  
  61.     if ( i == 0 && feof(fp) )
  62.     return NULL;
  63.  
  64.     return tmp;
  65. }
  66.  
  67. char    *mak_work(char *file)
  68. {
  69.     static char tmp[120];
  70.     char    *p;
  71.  
  72.     strcpy(tmp,file);
  73.     if ( (p = strrchr(tmp,'\\')) == NULL )
  74.     p = tmp;
  75.     if ( (p = strrchr(p,'.')) == NULL )
  76.     for ( p = tmp ; *p != '\0' ; p++ );
  77.     strcpy(p,".$0$");
  78.     return tmp;
  79. }
  80.  
  81. int    keyword(char *file,int sw)
  82. {
  83.     FILE    *ifp,*ofp;
  84.     int     ch;
  85.     int     pos=0,len=0;
  86.     short   crc;
  87.     char    *work;
  88.     char    tmp[120];
  89.  
  90.     if ( (ifp = fopen(file,"rb")) == NULL )
  91.     return ERR;
  92.  
  93.     if ( sw != FALSE ) {            /* if decode */
  94.     fread(tmp,1,7,ifp);
  95.     if ( strncmp(tmp,SECRET,7) != 0 ) {
  96.         fclose(ifp);
  97.         return ERR;
  98.     }
  99.     fread(&crc,sizeof(short),1,ifp);
  100.     }
  101.  
  102.     work = mak_work(file);
  103.     if ( (ofp = fopen(work,"wb")) == NULL ) {
  104.     fclose(ifp);
  105.     return ERR;
  106.     }
  107.  
  108.     if ( sw == FALSE ) {            /* if encode */
  109.     fwrite(SECRET,1,7,ofp);
  110.     fwrite(tmp,sizeof(short),1,ofp);    /* CRC dmy write */
  111.     }
  112.  
  113.     strcpy(tmp,macget("KEYWORD"));
  114.     if ( tmp[0] == '\0' )
  115.     strcpy(tmp,"PASS");
  116.     len = strlen(tmp);
  117.  
  118.     if ( sw == FALSE ) {            /* if encode */
  119.     Enc_init(ifp);
  120.     while ( (ch = Enc_char(ifp)) != EOF ) {
  121.         ch ^= tmp[pos];
  122.         putc(ch,ofp);
  123.         if ( ++pos >= len )
  124.         pos = 0;
  125.     }
  126.  
  127.     } else {
  128.     Dec_init();
  129.     while ( (ch = getc(ifp)) != EOF ) {
  130.         ch ^= tmp[pos];
  131.         Dec_char(ch,ofp);
  132.         if ( ++pos >= len )
  133.         pos = 0;
  134.     }
  135.     }
  136.  
  137.     if ( sw == FALSE ) {            /* if encode */
  138.     fflush(ofp);
  139.     fseek(ofp,7L,SEEK_SET);
  140.     fwrite(&t_crc,sizeof(short),1,ofp);    /* CRC write */
  141.     }
  142.  
  143.     ch = (ferror(ifp) || ferror(ofp)) ? ERR:FALSE;
  144.  
  145.     fclose(ifp);
  146.     fclose(ofp);
  147.  
  148.     if ( sw != FALSE && crc != t_crc )        /* if decode */
  149.     ch = ERR;
  150.  
  151.     if ( ch != FALSE )
  152.         unlink(work);
  153.     else {
  154.     unlink(file);
  155.     rename(work,file);
  156.     }
  157.  
  158.     return ch;
  159. }
  160. char    *copy_file_name(char *file)
  161. {
  162.     static char tmp[128];
  163.     char    *p;
  164.  
  165.     if ( (p = strrchr(file,'\\')) != NULL )
  166.     p++;
  167.     else if ( (p = strrchr(file,':')) != NULL )
  168.     p++;
  169.     else
  170.     p = file;
  171.     strcpy(tmp,p);
  172.     return tmp;
  173. }
  174. int    copy(char *file,char *new)
  175. {
  176.     int     i;
  177.     FILE    *ifp,*ofp;
  178.  
  179.     if ( (ifp = fopen(file,"rb")) == NULL )
  180.     return ERR;
  181.  
  182.     if ( (ofp = fopen(new,"wb")) == NULL ) {
  183.     fclose(ifp);
  184.     return ERR;
  185.     }
  186.  
  187.     while ( (i = getc(ifp)) != EOF )
  188.     putc(i,ofp);
  189.  
  190.     i = (ferror(ifp) || ferror(ofp)) ? ERR:FALSE;
  191.  
  192.     fclose(ifp);
  193.     fclose(ofp);
  194.     return i;
  195. }
  196.  
  197. int    merge(char *des,char *src)
  198. {
  199.     int     i;
  200.     FILE    *ifp,*ofp;
  201.  
  202.     if ( (ifp = fopen(src,"rb")) == NULL )
  203.     return ERR;
  204.  
  205.     if ( (ofp = fopen(des,"r+b")) == NULL ) {
  206.     fclose(ifp);
  207.     return ERR;
  208.     }
  209.  
  210.     while ( (i = getc(ofp)) != EOF && i != 0x1A );
  211.     if ( i == 0x1A ) {
  212.     fseek(ofp,(-1L),SEEK_CUR);
  213.     } else {
  214.     clearerr(ofp);
  215.     fseek(ofp,0L,SEEK_END);
  216.     }
  217.  
  218.     while ( (i = getc(ifp)) != EOF )
  219.     putc(i,ofp);
  220.  
  221.     i = (ferror(ifp) || ferror(ofp)) ? ERR:FALSE;
  222.  
  223.     fclose(ifp);
  224.     fclose(ofp);
  225.     return i;
  226. }
  227.  
  228. int    type(char *file)
  229. {
  230.     int     i,n,y;
  231.     FILE    *fp;
  232.     char    tmp[84];
  233.     static BLOCK *save=NULL;
  234.  
  235.     if ( file == NULL ) {
  236.     DSP_pop_vram(save);
  237.     save = NULL;
  238.     return FALSE;
  239.     }
  240.  
  241.     if ( (fp = fopen(file,"r")) == NULL )
  242.     return ERR;
  243.  
  244.     if ( (n = macval("TYPELINE")) <= 0 )
  245.     n = 8;
  246.     y = 28 - n;
  247.  
  248.     if ( save == NULL )
  249.         save = DSP_push_vram(0,y*16,639,463);
  250.  
  251.     i = 320 - strlen(file) * 4;
  252.     DSP_box(0,y*16,639,y*16+15,COL_LINE,COL_WHIS);
  253.     DSP_string(file,i,y*16+4,COL_WHIS2,COL_WHIS);
  254.  
  255.     for ( i = 0 ; i < n && read_line(tmp,fp) != NULL ; i++ )
  256.     putstr((i+y+1)*16*512,tmp);
  257.     for ( ; i < n ; i++ )
  258.     putstr((i+y+1)*16*512,"");
  259.  
  260.     fclose(fp);
  261.  
  262.     return FALSE;
  263. }
  264. int    secret_chk(char *file)
  265. {
  266.     FILE    *ifp;
  267.     char    tmp[16];
  268.  
  269.     if ( (ifp = fopen(file,"rb")) == NULL )
  270.     return FALSE;
  271.  
  272.     fread(tmp,1,7,ifp);
  273.     fclose(ifp);
  274.  
  275.     if ( strncmp(tmp,SECRET,7) == 0 )
  276.     return TRUE;
  277.     else
  278.     return FALSE;
  279. }
  280. char    *secret_file(char *file)
  281. {
  282.     FILE    *ifp,*ofp;
  283.     int     ch;
  284.     int     pos=0,len=0;
  285.     short   crc;
  286.     char    *work;
  287.     char    tmp[120];
  288.  
  289.     if ( (ifp = fopen(file,"rb")) == NULL )
  290.     return NULL;
  291.  
  292.     fread(tmp,1,7,ifp);
  293.     if ( strncmp(tmp,SECRET,7) != 0 ) {
  294.     fclose(ifp);
  295.     return NULL;
  296.     }
  297.     fread(&crc,sizeof(short),1,ifp);
  298.  
  299.     work = mak_work(file);
  300.     if ( (ofp = fopen(work,"wb")) == NULL ) {
  301.     fclose(ifp);
  302.     return NULL;
  303.     }
  304.  
  305.     strcpy(tmp,macget("KEYWORD"));
  306.     if ( tmp[0] == '\0' )
  307.     strcpy(tmp,"PASS");
  308.     len = strlen(tmp);
  309.  
  310.     Dec_init();
  311.     while ( (ch = getc(ifp)) != EOF ) {
  312.     ch ^= tmp[pos];
  313.     Dec_char(ch,ofp);
  314.     if ( ++pos >= len )
  315.         pos = 0;
  316.     }
  317.  
  318.     if ( ferror(ifp) || ferror(ofp) || crc != t_crc ) {
  319.     fclose(ifp);
  320.     fclose(ofp);
  321.     unlink(work);
  322.     return NULL;
  323.     }
  324.  
  325.     fclose(ifp);
  326.     fclose(ofp);
  327.     return work;
  328. }
  329. char    toup(char ch)
  330. {
  331.     return toupper(ch);
  332. }
  333. int     patmatch(char *nam,char *arg)
  334. {
  335.     int     i,j;
  336.     char    *p;
  337.  
  338.     if ( (p = strrchr(nam,'\\')) != NULL )
  339.     nam = p+1;
  340.  
  341.     for ( ; *arg != '\0' && *nam != '\0' ; ) {
  342.     i = toup(*(arg++)); j = toup(*(nam++));
  343.     if ( i == '*' ) {
  344.         do {
  345.         if ( (i = toup(*(arg++))) == '\0' )
  346.             return TRUE;
  347.         } while ( i == '*' || i == '?' );
  348.         do {
  349.         if ( j == i && patmatch(arg,nam) == TRUE )
  350.             return TRUE;
  351.         } while ( (j = toup(*(nam++))) != '\0' );
  352.         if ( i == '.' && *arg == '*' )
  353.         return TRUE;
  354.         else
  355.         return FALSE;
  356.     }
  357.     else if ( i != '?' && i != j )
  358.         return FALSE;
  359.     }
  360.     if ( *arg == '\0' && *nam == '\0' )
  361.         return TRUE;
  362.     else
  363.     return FALSE;
  364. }
  365. void    eof_chk(FILE *fp)
  366. {
  367.     int     ch;
  368.  
  369.     _setmode(fp,_BINARY);
  370.     while ( (ch = getc(fp)) != EOF && ch != 0x1A );
  371.     if ( ch == 0x1A ) {
  372.     fseek(fp,(-1L),SEEK_CUR);
  373.     } else {
  374.     clearerr(fp);
  375.     fseek(fp,0L,SEEK_END);
  376.     }
  377.     _setmode(fp,_TEXT);
  378. }
  379.